cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

project(recipe-03 LANGUAGES CXX Fortran)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(BLAS REQUIRED)
find_package(LAPACK REQUIRED)

## 新变量 wrap_BLAS_LAPACK_sources
set(wrap_BLAS_LAPACK_sources
        ${CMAKE_CURRENT_BINARY_DIR}/wrap_BLAS_LAPACK/CxxBLAS.cpp
        ${CMAKE_CURRENT_BINARY_DIR}/wrap_BLAS_LAPACK/CxxBLAS.hpp
        ${CMAKE_CURRENT_BINARY_DIR}/wrap_BLAS_LAPACK/CxxLAPACK.cpp
        ${CMAKE_CURRENT_BINARY_DIR}/wrap_BLAS_LAPACK/CxxLAPACK.hpp)

add_custom_command(
        OUTPUT
            ${wrap_BLAS_LAPACK_sources}
        COMMAND                                                                                 # 解压
            ${CMAKE_COMMAND} -E tar zxf ${CMAKE_CURRENT_SOURCE_DIR}/wrap_BLAS_LAPACK.tar.gz
        COMMAND
            ${CMAKE_COMMAND} -E touch ${wrap_BLAS_LAPACK_sources}
        WORKING_DIRECTORY                                                                       # 工作目录
            ${CMAKE_CURRENT_BINARY_DIR}
        DEPENDS                                                                                 # 依赖文件
            ${CMAKE_CURRENT_SOURCE_DIR}/wrap_BLAS_LAPACK.tar.gz
        COMMENT                                                                                 # 输出提示
            "Unpacking C++ wrappers for BLAS/LAPACK"
        VERBATIM                                                                                # 为生成器和平台生成正确的命令， 从而确保完全独立
)

add_library(math "")
# 添加源文件
target_sources(math
        PRIVATE                                                                                 # 源文件 私有
            ${CMAKE_CURRENT_BINARY_DIR}/wrap_BLAS_LAPACK/CxxLAPACK.cpp
            ${CMAKE_CURRENT_BINARY_DIR}/wrap_BLAS_LAPACK/CxxBLAS.cpp
        PUBLIC                                                                                  # 头文件 公开
            ${CMAKE_CURRENT_BINARY_DIR}/wrap_BLAS_LAPACK/CxxLAPACK.hpp
            ${CMAKE_CURRENT_BINARY_DIR}/wrap_BLAS_LAPACK/CxxBLAS.hpp
)
# 添加头文件目录
target_include_directories(math
        INTERFACE
            ${CMAKE_CURRENT_BINARY_DIR}/wrap_BLAS_LAPACK
)
# 添加链接库
target_link_libraries(math
        PUBLIC
            ${LAPACK_LIBRARIES}
)

add_executable(linear-algebra linear-algebra.cpp)
target_link_libraries(linear-algebra PRIVATE math)
